package com.wlz.behavioral.observer.threadwacth;


import com.wlz.behavioral.observer.IUndercover;
import com.wlz.behavioral.observer.Liubei;

public class UndercoverThread extends Thread {
    private Liubei iLeader;
    private IUndercover undercover;
    private String type;

    //通过构造函数传递参数， 我要监控的是谁， 谁来监控， 要监控什么
    public UndercoverThread(Liubei liubei, IUndercover undercover, String type) {
        this.iLeader = liubei;
        this.undercover = undercover;
        this.type = type;
    }

    @Override
    public void run() {
        while (true) {
            //监控是否在吃早餐
            if (this.type.equals("breakfast")) {
                //如果发现韩非子在吃饭， 就通知李斯
                if (this.iLeader.isHavingBreakfast()) {
                    this.undercover.report("刘备在吃饭");
                    //重置状态， 继续监控
                    this.iLeader.setHavingBreakfast(false);
                }
            } else {//监控是否在娱乐
                if (this.iLeader.isHavingFun()) {
                    this.undercover.report("刘备在蹦迪");
                    this.iLeader.setHavingFun(false);
                }
            }
        }
    }
}
